package com.itheima.controller;

import com.github.pagehelper.Page;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.RoleService;
import com.itheima.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    //获取角色信息列表
    @PostMapping("getList")
    public Result getList(@RequestBody Map<String,Object> roleVO){
        Page<Role> rolePage = roleService.findAll(roleVO);
        PageResult<Role> pageResult=new PageResult<>();
        pageResult.setTotal(rolePage.getTotal());
        pageResult.setRecords(rolePage.getResult());
        pageResult.setSize((long) rolePage.getPageSize());
        pageResult.setCurrent((long) rolePage.getPageNum());
        pageResult.setOptimizeCountSql(true);
        pageResult.setSearchCount(true);
        return new Result("success",pageResult);
    }

    //更新角色信息
    @PostMapping("/edit")
    public Result edit(@RequestBody Map<String,Object> roleVO){
        roleService.edit(roleVO);
        return new Result("success","操作成功");
    }

    //删除角色信息
    @PostMapping("/delete")
    public Result delete(@RequestBody Map<String,Object> roleVO){
        roleService.delete(roleVO);
        return new Result("success","操作成功");
    }

    //新增角色信息
    @PostMapping("/add")
    public Result add(@RequestBody Map<String,Object> roleVO){
        roleService.add(roleVO);
        return new Result("success","操作成功");
    }
}
